//package com.my.geojson.geotools.service.base.impl;
//
//import com.baomidou.mybatisplus.annotation.TableField;
//import com.baomidou.mybatisplus.annotation.TableName;
//import com.my.geojson.geotools.service.base.SimpleGeoToolService;
//import com.my.geojson.store.PostgisDataStore;
//import lombok.SneakyThrows;
//import org.geotools.data.DataStore;
//import org.geotools.data.collection.ListFeatureCollection;
//import org.geotools.data.simple.SimpleFeatureCollection;
//import org.geotools.data.simple.SimpleFeatureSource;
//import org.geotools.data.simple.SimpleFeatureStore;
//import org.geotools.feature.FeatureIterator;
//import org.geotools.feature.simple.SimpleFeatureBuilder;
//import org.geotools.filter.text.cql2.CQL;
//import org.geotools.geometry.jts.JTSFactoryFinder;
//import org.locationtech.jts.geom.Coordinate;
//import org.locationtech.jts.geom.GeometryFactory;
//import org.locationtech.jts.geom.Point;
//import org.opengis.feature.simple.SimpleFeature;
//import org.opengis.feature.simple.SimpleFeatureType;
//import org.opengis.filter.Filter;
//import org.opengis.filter.identity.FeatureId;
//import org.springframework.stereotype.Service;
//
//import java.lang.reflect.Field;
//import java.lang.reflect.Modifier;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.List;
//
//import static org.geotools.data.Transaction.AUTO_COMMIT;
//
//@Service
//public class SimpleGeoToolServiceImplFunctional<T>  implements SimpleGeoToolService<T>  {
//
//    @FunctionalInterface
//    interface GeotoolOperation {
//        void operation(SimpleFeatureStore simpleFeatureStore) throws Exception;
//    }
//    @SneakyThrows
//    private SimpleFeatureStore operate(GeotoolOperation geotoolOperation,Class<T> tClass) {
//        //获取表面
//        TableName name = tClass.getAnnotation(TableName.class);
//        String tableName = name.value();
//
//        DataStore postgisDataStore = PostgisDataStore.getInstance();
//        SimpleFeatureSource simpleFeatureSource = postgisDataStore.getFeatureSource(tableName);
//        SimpleFeatureStore featureStore = (SimpleFeatureStore) simpleFeatureSource;
//        //事务自动提交
//        featureStore.setTransaction(AUTO_COMMIT);
//        //这里不关注返回值--执行特定操作
//        geotoolOperation.operation(featureStore);
//        return featureStore;
//    }
//
//    @SneakyThrows
//    @Override
//    public boolean addPointFeature(Double lon, Double lat,Class<T> tClass) {
//        TableName name = tClass.getAnnotation(TableName.class);
//        String tableName = name.value();
//
//        DataStore postgisDataStore = PostgisDataStore.getInstance();
//        SimpleFeatureSource simpleFeatureSource = postgisDataStore.getFeatureSource(tableName);
//        SimpleFeatureType type = simpleFeatureSource.getSchema();
//
//        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type);
//
////        Field[] declaredFields = tClass.getDeclaredFields();
////        for (Field field : declaredFields) {
////            boolean aStatic = Modifier.isStatic(field.getModifiers());
////            if(aStatic){
////                continue;
////            }
////            String fieldName = field.getName();
////            if(fieldName.equals("id")){
////                continue;
////            }
////            field.setAccessible(true);
////            Object o = field.get(val);
//////            field.set(fieldName, field.get(val));
////            TableField annotation = field.getAnnotation(TableField.class);
////            if(annotation!=null){
////                fieldName = annotation.value();
////            }
////            featureBuilder.set(fieldName,field.get(val));
////        }
//////        featureBuilder.add(val);
//        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
//        Point point = geometryFactory.createPoint(new Coordinate(lon,lat));
//        //属性值顺序与SimpleFeatureType对应
////        List<Object> resultList = new ArrayList<>();
////        resultList.add(point);
////        resultList.add(5678);
////        resultList.add("erftg");
////        featureBuilder.addAll(resultList);
//        featureBuilder.set("name","李四");
//        featureBuilder.set("location","重庆市");
//        featureBuilder.set("lnglat", Arrays.asList("2.1","2.2","3.3").toArray(new String[3]));
//        featureBuilder.set("lnglat_geom",point);
//        //虽然有设置默认值，但是不加会报错。。
//        featureBuilder.set("is_deleted",0);
//        featureBuilder.set("revision",0);
////        featureBuilder.add(point);
//        //构建要素
////        SimpleFeature feature = featureBuilder.buildFeature("poi1");
//        SimpleFeature feature = featureBuilder.buildFeature(null);
//
//        List<SimpleFeature> features = new ArrayList<>();
//        features.add(feature);
//
//        SimpleFeatureCollection featureCollection =
//                new ListFeatureCollection(type,features);
//
//        SimpleFeatureStore featureStore = (SimpleFeatureStore) simpleFeatureSource;
//        featureStore.setTransaction(AUTO_COMMIT);
//        List<FeatureId> added = featureStore.addFeatures(featureCollection);
//        System.out.println( "Added "+ added );
//        return true;
//    }
//    @SneakyThrows
//    @Override
//    public boolean addFeature(Object val, Class<T> tClass) {
//        SimpleFeatureStore simpleFeatureStores = operate(simpleFeatureStore -> {
//            SimpleFeatureType type = simpleFeatureStore.getSchema();
//
//            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type);
//            Field[] declaredFields = tClass.getDeclaredFields();
//            for (Field field : declaredFields) {
//                boolean aStatic = Modifier.isStatic(field.getModifiers());
//                if(aStatic){
//                    continue;
//                }
//                String fieldName = field.getName();
//                if(fieldName.equals("id")){
//                    continue;
//                }
//                field.setAccessible(true);
//                TableField annotation = field.getAnnotation(TableField.class);
//                if(annotation!=null){
//                    fieldName = annotation.value();
//                }
//                featureBuilder.set(fieldName,field.get(val));
//            }
//            //构建要素
//            //SimpleFeature feature = featureBuilder.buildFeature("poi1");
//            SimpleFeature feature = featureBuilder.buildFeature(null);
//
//            List<SimpleFeature> features = new ArrayList<>();
//            features.add(feature);
//            SimpleFeatureCollection featureCollection =
//                    new ListFeatureCollection(type,features);
//
//            List<FeatureId> featureIds = simpleFeatureStore.addFeatures(featureCollection);
//        },tClass);
//        return true;
//    }
//
//    @SneakyThrows
//    @Override
//    public boolean removeFeatureByName(String name, Class<T> tClass) {
//        SimpleFeatureStore simpleFeatureStores = operate(simpleFeatureStore -> {
//        },tClass);
//        String format = String.format("name = '%s'", name);
//        Filter filter = CQL.toFilter(format);
//        simpleFeatureStores.removeFeatures(filter);
//        return true;
//    }
//    @SneakyThrows
//    @Override
//    public ArrayList<SimpleFeature> queryFeature(String filterStr,Class<T> tClass) {
//        ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
//
//        SimpleFeatureStore simpleFeatureStores = operate(simpleFeatureStore -> {
//        },tClass);
//        //filterStr形式 如  name='武汉大学' or code like 'tt123%'
//        Filter filter = CQL.toFilter(filterStr);
//        SimpleFeatureCollection result = simpleFeatureStores.getFeatures(filter);
//
//        FeatureIterator<SimpleFeature> itertor = result.features();
//        while (itertor.hasNext()) {
//            SimpleFeature feature = itertor.next();
//            featureList.add(feature);
//        }
//        itertor.close();
//        return featureList;
//    }
//
//    @Override
//    public boolean modifyFeature(Object val, Class<T> tClass) {
//        SimpleFeatureStore simpleFeatureStores = operate(simpleFeatureStore -> {
//            String filterStr=" name = '张三1212' ";
//            //修改name为张三1212的name为张大爷
////            String[] names=new String[2];
////            names[0]="StandName";
////            names[1]="the_geom";
////            Object[] values=new Object[2];
////            values[0]=featureName;
////            values[1]=geo;
//            String[] names=new String[2];
//            names[0]="name";
//            Object[] values=new Object[2];
//            values[0]="张大爷";
//            simpleFeatureStore.modifyFeatures(names, values,
//                    CQL.toFilter(filterStr));
//        },tClass);
//        return true;
//    }
//
//}
